package com.yuhang.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuhang.entity.HyAdmin;
import com.yuhang.entity.HyRepair;
import com.yuhang.entity.HyUser;
import com.yuhang.service.HyAdminService;
import com.yuhang.service.HyRepairService;
import com.yuhang.service.HyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuhang
 * @since 2021-03-11
 */
@Controller
@RequestMapping("/hy-admin")
public class HyAdminController {
    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;
    @Autowired
    private HyRepairService hyRepairService;
    @Autowired
    private HyAdminService hyAdminService;
    @Autowired
    private HyUserService hyUserService;

    @RequestMapping("/index")
    public String index(){
        return "/login.jsp";
    }

    @ResponseBody
    @RequestMapping("/login")
    public Map<String,Object> login(HttpSession session, String username, String password , String code) {
        Object sessionCode = session.getAttribute("code");
        if(code.equals(sessionCode)){

            QueryWrapper<HyAdmin> qw1 = new QueryWrapper();
            qw1.eq("username",username).eq("password",password);
            HyAdmin hyAdmin = hyAdminService.getOne(qw1);

            QueryWrapper<HyRepair> qw2 = new QueryWrapper();
            qw2.eq("username",username).eq("password",password);
            HyRepair hyRepair = hyRepairService.getOne(qw2);

            QueryWrapper<HyUser> qw3 = new QueryWrapper();
            qw3.eq("username",username).eq("password",password);
            HyUser hyUser = hyUserService.getOne(qw3);

            int state = 0;
            if(hyAdmin!=null){
                state = 1;
            }else if (hyRepair!=null){
                state = 2;
            }else if (hyUser!=null){
                state = 3;
            }

            if(state==0){
                Map<String, Object> map = new HashMap<>();
                map.put("code",1);
                map.put("message","用户名或密码错误！！！");
                return map;
            }else{
                if(state==1){
                    if(hyAdmin.getState()==1){
                        session.setAttribute("hyAdmin",hyAdmin);
                        session.setAttribute("state",state);
                        Map<String, Object> map = new HashMap<>();
                        map.put("code",0);
                        return map;
                    }else {
                        Map<String, Object> map = new HashMap<>();
                        map.put("code",3);
                        map.put("message","用户处于封禁状态！！！");
                        return map;
                    }
                }else if(state==2){
                    if(hyRepair.getState()==0){
                        session.setAttribute("hyRepair",hyRepair);
                        session.setAttribute("state",state);
                        Map<String, Object> map = new HashMap<>();
                        map.put("code",0);
                        return map;
                    }else {
                        Map<String, Object> map = new HashMap<>();
                        map.put("code",3);
                        map.put("message","用户处于封禁状态！！！");
                        return map;
                    }
                }else if(state==3){
                    session.setAttribute("hyUser",hyUser);
                    session.setAttribute("state",state);
                }
                Map<String, Object> map = new HashMap<>();
                map.put("code",0);
                return map;
            }
        }else{
            Map<String, Object> map = new HashMap<>();
            map.put("code",2);
            map.put("message","验证码错误！！！");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,10);
        //将验证码放入session
        session.setAttribute("code",lineCaptcha.getCode());
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @ResponseBody
    @RequestMapping("/showAllAdmin")
    public Map<String,Object> shoAllAdmin(@RequestParam(value = "page",defaultValue = "1")int pageNum,
                                          @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                          @RequestParam(value = "likename",defaultValue = "")String likename){
        IPage<HyAdmin> page = hyAdminService.selectByPage(pageNum, pageSize,likename);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        map.put("msg","");
        return map;
    }
    @ResponseBody
    @RequestMapping("/addAdmin")
    public Map<String,Object> addAdmin(HyAdmin hyAdmin){
        String username = hyAdmin.getUsername();;
        if(hyAdminService.selectByUsername(username)){
            hyAdminService.save(hyAdmin);
            Map<String, Object> map = new HashMap<>();
            map.put("code",0);
            return map;
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该用户名已存在！！！");
            return map;
        }
    }
    @ResponseBody
    @RequestMapping("/updateAdmin")
    public Map<String,Object> updateAdmin(HyAdmin hyAdmin){
        String username = hyAdmin.getUsername();
        Boolean aBoolean1 = hyAdminService.selectByUser(username);
        Boolean aBoolean2 = hyAdminService.selectByRepair(username);
        HyAdmin hyAdmin1 = hyAdminService.getById(hyAdmin.getId());
        QueryWrapper<HyAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<HyAdmin> list = hyAdminService.list(queryWrapper);
        Boolean aBoolean3 = false;
        if(list.size()>1){
            aBoolean3=false;
        }
        if(list.size()==1){
            HyAdmin hyAdmin2 = list.get(0);
            if(hyAdmin2.getId()==hyAdmin.getId()){
                aBoolean3=true;
            }
        }
        if(aBoolean1&&aBoolean2&&aBoolean3){
            hyAdminService.updateById(hyAdmin);
            Map<String, Object> map = new HashMap<>();
            map.put("code",0);
            return map;
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该用户名已存在！！！");
            return map;
        }

    }
    @ResponseBody
    @RequestMapping("/updateAdmin2")
    public Map<String,Object> updateAdmin2(HyAdmin hyAdmin,String newpassword,HttpSession session){
        String username = hyAdmin.getUsername();
        Boolean aBoolean1 = hyAdminService.selectByUser(username);
        Boolean aBoolean2 = hyAdminService.selectByRepair(username);
        HyAdmin hyAdmin1 = hyAdminService.getById(hyAdmin.getId());
        QueryWrapper<HyAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<HyAdmin> list = hyAdminService.list(queryWrapper);
        Boolean aBoolean3 = false;
        if(list.size()>1){
            aBoolean3=false;
        }
        if(list.size()==1){
            HyAdmin hyAdmin2 = list.get(0);
            if(hyAdmin2.getId()==hyAdmin.getId()){
                aBoolean3=true;
            }else{
                aBoolean3=false;
            }
        }
        if(aBoolean1&&aBoolean2&&aBoolean3){
            if(hyAdmin.getPassword().equals(hyAdmin1.getPassword())){
                if(newpassword!=null&&newpassword!=""){
                    hyAdmin.setPassword(newpassword);
                }
                hyAdminService.updateById(hyAdmin);
                session.setAttribute("hyAdmin",hyAdminService.getById(hyAdmin));
                Map<String, Object> map = new HashMap<>();
                map.put("code",0);
                return map;
            }else{
                Map<String, Object> map = new HashMap<>();
                map.put("code",2);
                map.put("message","密码错误！！！");
                return map;
            }
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该用户名已存在！！！");
            return map;
        }

    }
    @ResponseBody
    @RequestMapping("/deleteAdmin")
    public Map<String,Object> deleteAdmin(Integer id){
        hyAdminService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @ResponseBody
    @RequestMapping("/uploadImage")
    public Map<String,Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path,file.getOriginalFilename()));
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg",file.getOriginalFilename());
        return map;
    }

    @ResponseBody
    @RequestMapping("/logout")
    public Map<String,Object> logout(HttpSession session) {
        session.invalidate();
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        return map;
    }

}
